const { list, add, find, deleteRecord, carpoolReport } = require('./data/carpools')
const { getLoginUser } = require('./data/login')

const base = '/shopmovie'

module.exports = [{
  url: `${base}/carpools/dm/report`,
  type: 'post',
  response: config => {
    const item = carpoolReport(config.body)
    return { success: true, data: item }
  }
}, {
  url: `${base}/carpools`,
  type: 'post',
  response: config => {
    add(config.body)
    return { success: true }
  }
}, {
  url: `${base}/carpools`,
  type: 'get',
  response: config => {
    const { page, limit, shopMovieId, orgUnitId, status, start, end } = config.query
    return list(page, limit, orgUnitId, shopMovieId, status, start, end)
  }
}, {
  url: `${base}/carpools/[1-9][0-9]*/confirmor`,
  type: 'put',
  response: config => {
    const id = config.url.match(/[1-9][0-9]*/)
    const r = find(id)
    if (r) {
      const user = getLoginUser()
      if (user) {
        r.confirmorId = user.id
        r.confirmTime = new Date()
        return { success: true }
      } else {
        return { success: false, message: '用户未登录' }
      }
    } else {
      return { success: false, message: '记录不存在' }
    }
  }
}, {
  url: `${base}/carpools/[1-9][0-9]*`,
  type: 'put',
  response: config => {
    const id = config.url.match(/[1-9][0-9]*/)
    const r = find(id)
    if (r) {
      const { shopMovieId, startTime, title, tag, playerCount, version } = config.body
      if (version === r.version) {
        r.shopMovieId = shopMovieId
        r.startTime = startTime
        r.title = title
        r.tag = tag
        r.playerCount = playerCount
        return { success: true }
      } else {
        return { success: false, message: '数据版本不一致,请刷新后重试' }
      }
    } else {
      return { success: false, message: '记录不存在' }
    }
  }
}, {
  url: `${base}/carpools/[1-9][0-9]*`,
  type: 'delete',
  response: config => {
    const id = config.url.match(/[1-9][0-9]*/)
    deleteRecord(id)
    return { success: true }
  }
}]
